<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/26
 * Time: 9:54
 */
namespace app\api\controller;
use think\Db;
class Topic  extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
    //话题分类
    public function classify(){
        $res = Db::table('up_topic_type')->field('topic_type_id,topic_type_name,topic_type_img')->where('topic_type_status',1)->order('topic_type_sort asc')->select();
        return json(['status' => 200, 'msg' =>$res]);
    }

    /**
     *小区列表
     */
    public function village(){
        $xq_name = input('xq_name');
        $where = [];
        if(isset($xq_name) && $xq_name != ''){
            $where['xq_name'] = ['like', '%' . $xq_name . '%'];
        }
        $xq = Db::table('up_xq')->where($where)->select();
        foreach($xq as $k => $v){
            $xq[$k]['letter'] = $this->Getzimu($v['xq_name']);
        }
        $new_names = array_column($xq, 'letter');
        array_multisort($new_names, SORT_ASC, $xq);
        $start = '';
        $res = array();
        foreach ($xq as $k => $v) {
            if ($v['letter'] != $start) {
                $start = $v['letter'];
            }
            $res[$start][] = $v;
        }
        $jing = array();
        if(isset($res['#'])){
            $jing['#'] = $res['#'];
            unset($res['#']);
        }
        $xq = array_merge($res,$jing);
        return json(['status' => 200, 'msg' =>$xq]);
    }

    /***
     * 列表数据
     */
    public function index(){
        extract(input());
        $type=input('param.type');//1最新 2最热
        $page = input('param.page')?input('param.page'):1;
        $topic_type_id = input('param.topic_type_id');
        $xq_id = input('param.xq_id');
        if($topic_type_id!=''){
            $map['a.topic_type'] = $topic_type_id;
        }
        if($xq_id!=''){
           $map['a.topic_xq'] = $xq_id;
        }
        $num = 20;
        $limit = ($page-1)*$num;
        $topic_id_list = Db::table('up_topic_qx')->where('user_id',$user_id)->column('topic_id');
        if($type==1){//最新
            $map['topic_is_show'] = 1;
            $map['topic_id'] = array('not in',$topic_id_list);
            $info =Db::table('up_topic')->alias('a')
                ->join('up_u_user uuu','uuu.user_id=a.topic_user_id','left')
                ->where($map)
                ->field('a.topic_id,a.topic_title,a.topic_content,a.topic_img,a.topic_fb_time,a.topic_user_id,
                 uuu.user_username,uuu.user_image')
                 ->order('a.topic_fb_time desc')
                ->limit($limit,$num)->select();
        }elseif ($type==2){//最热
            $map['topic_is_show'] = 1;
            $map['topic_id'] = array('not in',$topic_id_list);
            $info =Db::table('up_topic')->alias('a')
                ->join('up_u_user uuu','uuu.user_id=a.topic_user_id','left')
                ->where($map)
                ->field('a.topic_id,a.topic_title,a.topic_content,a.topic_img,a.topic_fb_time,a.topic_user_id,
                 uuu.user_username,uuu.user_image')
                ->order('a.topic_num_sort desc,a.topic_fb_time desc')
                ->limit($limit,$num)->select();
        }
        foreach ($info as $key=>&$vo){
            //统计留言,收藏,点赞数量
            $dz_num = Db::table('up_topic_dz')->where('topic_id',$vo['topic_id'])->count();
            $whereMap['topic_id'] = $vo['topic_id'];
            $whereMap['topic_status'] = 1;
            $message_num = Db::table('up_topic_message')->where($whereMap)->count();
            $like_num = Db::table('up_topic_like')->where('topic_id',$vo['topic_id'])->count();
            $vo['dz_num'] = $dz_num;
            $vo['message_num'] = $message_num;
            $vo['like_num'] = $like_num;
            //判断这个用户是否收藏
            $where['topic_like_user'] = $user_id;
            $where['topic_id'] = $vo['topic_id'];
            $info1 = Db::table('up_topic_like')->where($where)->find();
            if($info1['topic_like_id']!=''){//证明收藏了
                $vo['like'] = 1;
            }else{
                $vo['like'] = 2;
            }
            //判断这个用户是否点赞
            $where1['topic_dz_user'] = $user_id;
            $where1['topic_id'] = $vo['topic_id'];
            $info2 = Db::table('up_topic_dz')->where($where1)->find();
            if($info2['topic_dz_id']!=''){//证明点赞了
                $vo['dz'] = 1;
            }else{
                $vo['dz'] = 2;
            }
        }
        return json(['status' => 200, 'msg' =>$info]);
    }
    public function qxTopic(){
        extract(input());
        $data['user_id'] = $user_id;
        $data['topic_id'] = input('param.topic_id');
        $res = Db::table('up_topic_qx')->insertGetId($data);
        if($res){
            return json(['status' => 200, 'msg' =>"操作成功"]);
        }else{
            return json(['status' => 200, 'msg' =>"操作失败"]);
        }
    }
    /**
     * 点赞
     */
    public function giveUp(){
        extract(input());
        $topic_id = input('param.topic_id');
        $data['topic_dz_user'] = $user_id;
        $data['topic_id'] = $topic_id;
        $data['topic_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_topic_dz')->insertGetId($data);
        //$list = Db::table('up_topic')->where('topic_id',$topic_id)->setInc('topic_num',1);
        if($res){
            return json(['status' => 200, 'msg' =>"操作成功"]);
        }else{
            return json(['status' => 200, 'msg' =>"操作失败"]);
        }
    }

    /**
     * 取消点赞
     */
    public function giveDown(){
        extract(input());
        $topic_id = input('param.topic_id');
        $where['topic_dz_user'] = $user_id;
        $where['topic_id'] = $topic_id;
        $res = Db::table('up_topic_dz')->where($where)->delete();
        //$list = Db::table('up_topic')->where('topic_id',$topic_id)->setDec('topic_num',1);

        if($res){
            return json(['status' => 200, 'msg' =>"操作成功"]);
        }else{
            return json(['status' => 200, 'msg' =>"操作失败"]);
        }
    }

    //收藏
    public function likeUp(){
        extract(input());
        $topic_id = input('param.topic_id');
        $data['topic_like_user'] = $user_id;
        $data['topic_id'] = $topic_id;
        $data['topic_like_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_topic_like')->insertGetId($data);
        //$list = Db::table('up_topic')->where('topic_id',$topic_id)->setInc('topic_num',1);
        if($res){
            return json(['status' => 200, 'msg' =>"操作成功"]);
        }else{
            return json(['status' => 200, 'msg' =>"操作失败"]);
        }
    }

    /**
     * 取消收藏
     */
    public function likeDown(){
        extract(input());
        $topic_id = input('param.topic_id');
        $where['topic_like_user'] = $user_id;
        $where['topic_id'] = $topic_id;
        $res = Db::table('up_topic_like')->where($where)->delete();
        //$list = Db::table('up_topic')->where('topic_id',$topic_id)->setDec('topic_num',1);
        if($res){
            return json(['status' => 200, 'msg' =>"操作成功"]);
        }else{
            return json(['status' => 200, 'msg' =>"操作失败"]);
        }
    }

    //举报类型
    public function  reportType(){
        $where['report_type_status'] = 1;
        $where['is_del'] = 1;
        $res = Db::table('up_topic_report_type')->where($where)->field('report_type_id,report_type_name')->select();
        return json(['status' => 200, 'msg' =>$res]);
    }

    //举报
    public function report(){
        extract(input());
        $topic_id = input('param.topic_id');
        $report_type_id = input('param.report_type_id');
        $report_content = input('param.report_content');
        $report_img = input('param.report_img');
        $data['r_user_id'] = $user_id;
        $data['topic_id'] = $topic_id;
        $data['report_type_id'] = $report_type_id;
        $data['report_content'] = $report_content;
        $data['report_img'] = $report_img;
        $data['report_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_topic_report')->insertGetId($data);
        if($res){
            return json(['status' => 200, 'msg' =>"举报成功"]);
        }else{
            return json(['status' => 200, 'msg' =>"举报失败"]);
        }
    }
    //发布话题
    public function fbTopic(){
        extract(input());
        $topic_type = input('param.topic_type_id');
        $topic_xq = input('param.xq_id');
        $topic_title = input('param.topic_title');
        $topic_content = input('param.topic_content');
        $topic_img = input('param.topic_img');

        $data['topic_user_id'] = $user_id;
        $data['topic_type'] = $topic_type;
        $data['topic_xq'] = $topic_xq;
        $data['topic_title'] = $topic_title;
        $data['topic_content'] = $topic_content;
        $data['topic_img'] = $topic_img;
        $data['topic_fb_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_topic')->insertGetId($data);
        if($res){
            return json(['status' => 200, 'msg' =>"发布成功"]);
        }else{
            return json(['status' => 200, 'msg' =>"发布失败"]);
        }
    }
     //删除留言,收藏,话题,留言回复
    public function delTopic(){
        extract(input());
        $type = input('param.type');//1话题  2留言  3收藏  4留言回复
        $id = input('param.id');
        if($type==1){//删除话题
            Db::table('up_topic')->where('topic_id','IN',$id)->delete();
            //查询这个话题留言数量
            $whereMap['topic_id'] = array('IN',$id);
            $whereMap['topic_status'] = 1;
            $count = Db::table('up_topic_message')->where('topic_id','IN',$id)->count();
            if($count>0){
                //查询这个留言ID
                $whereMap1['topic_id'] = array('IN',$id);
                $whereMap1['topic_status'] = 1;
                $topic_message = Db::table('up_topic_message')->where($whereMap1)->column('topic_message_id');
                //删除留言
                Db::table('up_topic_message')->where('topic_id','IN',$id)->delete();
                //查询这个留言回复的数量
                $count1 = Db::table('up_topic_reply')->where('reply_message_id','IN',$topic_message)->count();
                if($count1>0){
                    Db::table('up_topic_reply')->where('reply_message_id','IN',$topic_message)->setField('reply_status',2);
                }
            }
        }elseif ($type==2){//删除留言
            Db::table('up_topic_message')->where('topic_message_id','IN',$id)->delete();
            //查询这个留言回复的数量
            $count1 = Db::table('up_topic_reply')->where('reply_message_id','IN',$id)->count();
            if($count1>0){
                Db::table('up_topic_reply')->where('reply_message_id','IN',$id)->setField('reply_status',2);
            }
        }elseif ($type==3){//收藏
            Db::table('up_topic_like')->where('topic_like_id','IN',$id)->delete();
        }elseif($type==4){
            Db::table('up_topic_reply')->where('reply_id','IN',$id)->delete();
        }
        return json(['status' => 200, 'msg' =>"操作成功"]);
    }

    //话题详情
    public function detailTopic(){
        extract(input());
        $topic_id = input('param.topic_id');
        $page = input('param.page')?input('param.page'):1;
        $num = 50;
        $limit = ($page-1)*$num;
        $res = Db::table('up_topic')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.topic_user_id','left')
            ->join('up_xq ux','ux.xq_id = a.topic_xq','left')
            ->join('up_topic_type utt','utt.topic_type_id = a.topic_type','left')
            ->field('uuu.user_username,uuu.user_image,ux.xq_name,utt.topic_type_name,
            a.topic_id,a.topic_title,a.topic_img,a.topic_content,a.topic_fb_time, a.topic_xq, a.topic_type')
            ->where('a.topic_id',$topic_id)
            ->find();
        //统计留言,收藏,点赞数量
        $dz_num = Db::table('up_topic_dz')->where('topic_id',$topic_id)->count();
        $like_num = Db::table('up_topic_like')->where('topic_id',$topic_id)->count();
        $res['dz_num'] = $dz_num;
        $res['like_num'] = $like_num;
        //判断这个用户是否收藏
        $whereMap1['topic_like_user'] = $user_id;
        $whereMap1['topic_id'] = $topic_id;
        $infoData1 = Db::table('up_topic_like')->where($whereMap1)->find();
        if($infoData1['topic_like_id']!=''){//证明收藏了
            $res['like'] = 1;
        }else{
            $res['like'] = 2;
        }
        //判断这个用户是否点赞
        $whereMap2['topic_dz_user'] = $user_id;
        $whereMap2['topic_id'] = $topic_id;
        $infoData2 = Db::table('up_topic_dz')->where($whereMap2)->find();
        if($infoData2['topic_dz_id']!=''){//证明点赞了
            $res['dz'] = 1;
        }else{
            $res['dz'] = 2;
        }


        //统计这个话题有多少留言
        $where['topic_id'] = $topic_id;
        $where['topic_status'] = 1;
        $num = Db::table('up_topic_message')->where('topic_id',$topic_id)->count();
        $res['num'] = $num;
        //查询这个评论
        $list = Db::table('up_topic_message')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.topic_message_user','left')
            ->field('uuu.user_username,uuu.user_image,
            a.topic_message_id,a.topic_message_user,
            a.topic_message_content,a.topic_message_time')
            ->where('a.topic_status',1)
            ->where('a.topic_id',$topic_id)->limit($limit,$num)->select();
        // 查询这个留言有多少回复
        foreach ($list as $key=>&$vo){
            $info = Db::table('up_topic_reply')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
                ->field('uuu.user_username,uuu.user_image,a.reply_id,a.reply_user_id,
                a.reply_message_id,a.reply_type,a.reply_content,a.reply_time,a.reply_reply_id')
                ->where('a.reply_message_id',$vo['topic_message_id'])
                ->where('a.reply_status',1)
                ->limit(3)
                ->select();
            foreach ($info as $kk=>&$vv){
                if($vv['reply_type']==2){
                    $info1 =Db::table('up_topic_reply')->where('reply_id',$vv['reply_reply_id'])->find();
                    $user_username = Db::table('up_u_user')->where('user_id',$info1['reply_user_id'])->value('user_username');
                    $vv['username'] = $user_username;
                }else{
                    $vv['username'] = '';
                }
            }
            $vo['reply'] = $info;
        }
        $res['list'] = $list;
        return json(['status' => 200, 'msg' =>$res]);
    }
    //查询更多评论回复
    public function moreTopic(){
        //查询这个评论
        $topic_message_id = input('param.topic_message_id');
        $page = input('param.page')?input('param.page'):1;
        $num = 50;
        $limit = ($page-1)*$num;
        $list = Db::table('up_topic_message')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.topic_message_user','left')
            ->field('uuu.user_username,uuu.user_image,
            a.topic_message_id,a.topic_message_user,
            a.topic_message_content,a.topic_message_time')
            ->where('a.topic_message_id',$topic_message_id)->find();
        $info = Db::table('up_topic_reply')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
            ->field('uuu.user_username,uuu.user_image,a.reply_id,a.reply_user_id,
                a.reply_message_id,a.reply_type,a.reply_content,a.reply_time,a.reply_reply_id')
            ->where('a.reply_message_id',$topic_message_id)
            ->where('a.reply_status',1)
            ->limit($limit,$num)
            ->select();
        foreach ($info as $kk=>&$vv){
            if($vv['reply_type']==2){
                $info1 =Db::table('up_topic_reply')->where('reply_id',$vv['reply_reply_id'])->find();
                $user_username = Db::table('up_u_user')->where('user_id',$info1['reply_user_id'])->value('user_username');
                $vv['username'] = $user_username;
            }else{
                $vv['username'] = '';
            }
        }
        $list['list'] = $info;
        return json(['status' => 200, 'msg' =>$list]);
    }
    /**
     * 评论
     * @return \think\response\Json
     * comment_info_id     被评论的风讯ID
     * comment_content     评论的内容
     */
    public function commentInfo(){
        extract(input());
        $topic_id = input('topic_id');
        $comment_content = input('comment_content');
        $comment = [
            'topic_message_user' => $user_id,
            'topic_id' => $topic_id,
            'topic_message_content' => $comment_content,
            'topic_message_time' => date('Y-m-d H:i:s',time())
        ];
        $res = Db::table('up_topic_message')->insert($comment);
        if($res){
            return json(['status' => 200, 'msg' => '提交成功']);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }

    /**
     * 回复
     * @return \think\response\Json
     * reply_type    回复类型   1 回复评论   2 回复的回复
     * reply_comment_id   评论的ID
     * reply_content      回复内容
     * reply_reply_id     回复的回复ID，如果回复的是评论，则传0
     */
    public function reply(){
        extract(input());
        $reply_type = input('reply_type'); // 1 评论的回复   2 回复的回复
        $topic_message_id = input('topic_message_id');
        $reply_content = input('reply_content');
        $reply_reply_id = input('reply_reply_id'); // 如果回复的是评论，则传0
        $reply= [
            'reply_type' => $reply_type,
            'reply_user_id' => $user_id,
            'reply_message_id' => $topic_message_id,
            'reply_reply_id' => $reply_reply_id,
            'reply_content' => $reply_content,
            'reply_time' => date('Y-m-d H:i:s',time())
        ];
        $res = Db::table('up_topic_reply')->insert($reply);
        if($res){
            return json(['status' => 200, 'msg' => '提交成功']);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    public function isTopic(){
        extract(input());
        $user_topic = Db::table('up_u_user')->where('user_id',$user_id)->value('user_topic');
        return json(['status' => 200, 'msg' => $user_topic]);

    }

}